package com.xh.sys.controller;

import com.xh.common.R;
import com.xh.sys.entity.SysCommentBack;
import com.xh.sys.service.SysCommentBackService;
import com.xh.sys.vo.webform.SysCommentBackQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 浏览评论回复功能下载_关联信息表 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/sys-comment-back")
@RequiredArgsConstructor
@Validated
public class SysCommentBackController {
    private final SysCommentBackService sysCommentBackService;

    /**
     * 保存/增加浏览次数,下载次数
     * 1外联的查询 查到更新 查不到新增
     *
     * @param tosave
     * @return
     */
    @PostMapping("/save")
    public R<SysCommentBack> save(@RequestBody
                                  @Validated SysCommentBack tosave) {

        boolean saveFlag = sysCommentBackService.saveSysCommentBack(tosave);

        if (saveFlag) {
            return R.success(sysCommentBackService.getById(tosave.getId()));
        }

        return R.failed("/sys-comment-back/save:保存失败");
    }

    /**
     * 主要用来更新 浏览量 下载量
     * <p>
     * v_d =0 浏览量
     * v_d =1 下载量
     * 其他暂不处理
     *
     * @return
     */
    @PostMapping("/updateById/{id}")
    public R<SysCommentBack> updateById(@PathVariable
                                        @Validated @NotNull Integer id,
                                        @RequestParam(value = "v_d")
                                        @Validated
                                        @NotNull Integer vd) {

        SysCommentBack byId = sysCommentBackService.getById(id);
        if (null == byId) {
            return R.failed("/sys-comment-back/updateById:id不存在");
        }

        if (0 == vd.intValue()) {
            byId.setViewedNumber(byId.getViewedNumber() + 1);
        }
        if (1 == vd.intValue()) {
            byId.setDownloadNumber(byId.getDownloadNumber() + 1);
        }
        boolean saveFlag = sysCommentBackService.updateById(byId);

        if (saveFlag) {
            return R.success("成功");
        }

        return R.failed("/sys-comment-back/updateById:保存失败");
    }

    /**
     * 加载评论主体 主题的第一步
     * 用于查看浏览量 下载量 关联下级评论
     */
    @PostMapping("/queryByForeign")
    public R<SysCommentBack> queryByForeign(@RequestBody
                                            @Validated
                                                    SysCommentBackQuery sysCommentBackQuery) {
        SysCommentBack one = sysCommentBackService.queryByForeign(sysCommentBackQuery);
        return R.success(one);
    }

    /**
     * 删除评论主题  暂时不用
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public R<SysCommentBack> del(@RequestBody
                                 @Validated
                                 @NotEmpty(message = "ids 集合不能空")
                                         List<Integer> ids) {
        sysCommentBackService.removeByIds(ids);
        return R.success("删除成功");
    }

}
